public class Test {
    public static void main(String[] args) {

    }
}
class Solution {
    public int longestOnes(int[] nums, int k) {
        int ans = 0;
        int count = 0;
        for(int i=0, j=0; j<nums.length; j++){
            count += 1 - nums[j];
            while(count > k){
                count -= 1-nums[i++];
            }
            ans = Math.max(ans,j-i+1);
        }
        return ans;
    }
}
class Solution {
    public int longestOnes(int[] nums, int k) {
        int ans = 0;
        int count = 0;
        for(int i=0, j=0; j<nums.length; j++){
            if(nums[j]!=1){
                if(count <= k){
                    count++;
                    if(count <= k)
                        ans = Math.max(ans,j-i+1);
                    else {
                        ans = Math.max(ans,j-i);
                        while(i<=j && nums[i]==1){
                            i++;
                        }
                        i++;
                        count--;
                    }
                }
            }else{
                ans = Math.max(ans,j-i+1);
            }
        }
        return ans;
    }
}